package com.fuqi.android.zhongcai.service;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.base.Constants;
import com.fuqi.android.zhongcai.base.request.api.FindSubNodeApi;
import com.fuqi.android.zhongcai.base.request.api.GetStaffApi;
import com.fuqi.android.zhongcai.base.request.api.ProductByTypeApi;
import com.fuqi.android.zhongcai.base.request.result.GetStaffResult;
import com.fuqi.android.zhongcai.base.request.result.ProductIntroResult;
import com.fuqi.android.zhongcai.business.shuig.OrderDict;
import com.fuqi.android.zhongcai.sql.Dic;
import com.fuqi.android.zhongcai.sql.Worker;
import com.fuqi.android.zhongcai.sql.dao.SQLDao;
import com.fuqi.android.zhongcai.utils.LogUtils;
import com.fuqi.android.zhongcai.utils.PinyinUtils;
import com.fuqi.framework.gson.GsonHelper;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;

import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.ArrayList;
import java.util.List;

import static com.fuqi.android.zhongcai.base.Constants.TYPE_DI_NUAN;
import static com.fuqi.android.zhongcai.base.Constants.TYPE_SHUI_GUAN;

/**
 * Created by Bruce on 2016/11/3.
 */
public class DicSyncService extends IntentService{

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public DicSyncService(String name) {
        super(name);
    }
    public DicSyncService() {
        super("DicSyncService");
    }
    @Override
       protected void onHandleIntent(Intent intent) {
        RequestManager.getInstance().call(new FindSubNodeApi(new FindSubNodeApi.FindSubNodeParams(OrderDict.DNSYType.SJ_AZNR),
                respChouChaListener, errorListener));

        RequestManager.getInstance().call(new FindSubNodeApi(new FindSubNodeApi.FindSubNodeParams(OrderDict.DNSYType.SJ_BKAZYY),
                respChouChaListener, errorListener));

        RequestManager.getInstance().call(new FindSubNodeApi(new FindSubNodeApi.FindSubNodeParams(OrderDict.DNSYType.SJ_DNGDGG),
                respChouChaListener, errorListener));

        RequestManager.getInstance().call(new FindSubNodeApi(new FindSubNodeApi.FindSubNodeParams(OrderDict.DNSYType.SJ_SKFS),
                respChouChaListener, errorListener));

        RequestManager.getInstance().call(new ProductByTypeApi(new ProductByTypeApi.ProductIntroParams(TYPE_SHUI_GUAN),
                respGongYiListener, errorListener));

        RequestManager.getInstance().call(new ProductByTypeApi(new ProductByTypeApi.ProductIntroParams(TYPE_DI_NUAN),
                respGongYiListener, errorListener));


        RequestManager.getInstance().call(new GetStaffApi(new GetStaffApi.GetStaffParams(Constants.ROLE_SGGR), respWorkersListener, errorListener));

    }

    Response.Listener<Dic[]> respChouChaListener = new Response.Listener<Dic[]>() {

        @Override
        public void onResponse(Dic[] response) {
            if (HandleCode.requestSuccess()) {
                SQLDao.getInstance(getApplicationContext()).insertDic(response);
                LogUtils.i( "response OrderListResult: " + GsonHelper.writeValue(response));
            }
        }
    };

    Response.Listener<ProductIntroResult[]> respGongYiListener = new Response.Listener<ProductIntroResult[]>() {

        @Override
        public void onResponse(ProductIntroResult[] response) {
            if (HandleCode.requestSuccess()) {
                SQLDao.getInstance(getApplicationContext()).insertDic(parseData(response));
                LogUtils.i( "response OrderListResult: " + GsonHelper.writeValue(parseData(response)));
            }
        }
    };

    Response.Listener<GetStaffResult[]> respWorkersListener = new Response.Listener<GetStaffResult[]>() {
        @Override
        public void onResponse(GetStaffResult[] response) {
            if (HandleCode.requestSuccess()) {
                if (response != null && response.length > 0){
                    List<Worker> mWorkers = new ArrayList<>();
                    for (int i = 0; i < response.length; i++) {
                        Worker worker = null;
                        try {
                            worker = new Worker(response[i].getUserId(), response[i].getName(), PinyinUtils.toPinYin(response[i].getName()),response[i].getPhone());
                        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                            badHanyuPinyinOutputFormatCombination.printStackTrace();
                        }
                        mWorkers.add(worker);
                    }
                    SQLDao.getInstance(getApplicationContext()).insertWorker(mWorkers);
                }

                LogUtils.i("juner respListener response : ");
            }
        }
    };

    Dic[] parseData(ProductIntroResult[] response){
        Dic[] adic = new Dic[response.length];
        for (int i = 0;i<response.length;i++){
            ProductIntroResult product = response[i];
            Dic dic = new Dic(product.getProName(),product.getProductId(),product.getOrderType());
            adic[i] = dic;
        }
        return adic;
    }




    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());

        }
    };
}
